package com.warren.controller;

import com.warren.common.ResponseResult;
import com.warren.entity.User;
import com.warren.entity.vo.UserQueryBean;
import com.warren.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author warren
 * @data: 2022/3/27 4:54 PM
 * @decription:
 */
@RestController
@RequestMapping("/user")
@Api("user api")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation("add user")
    @PostMapping("add")
    //@RequestBody 可以添加可以不加
    public ResponseResult<User> add(@RequestBody User user) {
        System.out.println(user);
        if (user.getId() == null) {
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            userService.save(user);
        } else {
            user.setUpdateTime(LocalDateTime.now());
            userService.update(user);
        }
        return ResponseResult.success(userService.findById(user.getId()));
    }

    @ApiOperation("Query User One")
    @GetMapping("edit/{userId}")
    public ResponseResult<User> edit(@PathVariable("userId") Long userId) {
        return ResponseResult.success(userService.findById(userId));
    }

    @ApiOperation("Query User List")
    @GetMapping("list")
    public ResponseResult<List<User>> list(UserQueryBean userQueryBean) {
        return ResponseResult.success(userService.findList(userQueryBean));
    }

    @ApiOperation("hello")
    @GetMapping("hello")
    public String get() {
        String traceId = MDC.get("traceId");
        return "hello" + traceId;
    }
}
